import java.util.LinkedList;
import java.util.Queue;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dell
 * Date: 2024-01-10
 * Time: 12:10
 */
class MyStack {
    private Queue<Integer> queue1;
    private Queue<Integer> queue2;
    public MyStack() {
        queue1=new LinkedList<>();
        queue2=new LinkedList<>();
    }
    //void push(int x) 将元素 x 压入栈顶。
    public void push(int x) {
        if (!queue1.isEmpty()){
            queue1.offer(x);
        } else if (!queue2.isEmpty()) {
            queue2.offer(x);
        }else {
            queue1.offer(x);
        }
    }
    //int pop() 移除并返回栈顶元素。
    public int pop() {
        if (empty()){
            return -1;
        }
        if (!queue1.isEmpty()){
            int unsize=queue1.size();
            for (int i=0;i<unsize-1;i++){
                int x=queue1.poll();
                queue2.offer(x);
            }
            return queue1.poll();
        }
        if (!queue2.isEmpty()){
            int unsize=queue2.size();
            for (int i=0;i<unsize-1;i++){
                int x=queue2.poll();
                queue1.offer(x);
            }
            return queue2.poll();
        }
        return -1;
    }
    //int top() 返回栈顶元素。
    public int top() {
        if (empty()){
            return -1;
        }
        if (!queue1.isEmpty()){
            int unsize=queue1.size();
            int x=-1;
            for (int i=0;i<unsize;i++){
                x=queue1.poll();
                queue2.offer(x);
            }
            return x;
        }
        if (!queue2.isEmpty()){
            int unsize=queue2.size();
            int x=-1;
            for (int i=0;i<unsize;i++){
                x=queue2.poll();
                queue1.offer(x);
            }
            return x;
        }
        return -1;
    }

    //boolean empty()
    public boolean empty() {
        return queue1.isEmpty()&&queue2.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */